package test.controller;

import test.mapper.AticleMapper;
import test.util.InputUtil;
import test.vo.Article;

import java.util.List;
import java.util.Scanner;

public class ArticleController {
    private AticleMapper articleMapper = new AticleMapper();
    public  void main(String[] args) {
        while (true){
        System.out.println("欢迎来到文章管理系统");
        System.out.println("请输入操作编号:");
        System.out.println("1.保存文章");
        System.out.println("2.删除文章");
        System.out.println("3.查询指定文章");
        System.out.println("4.查询全部文章");
        Scanner scanner = new Scanner(System.in);
        String num = scanner.nextLine();
        switch (num) {
            case "1":
                writeArticle();
                System.out.println("保存文章操作");
                break;
            case "2":
                deleteById();
                System.out.println("删除文章操作");
                break;
            case "3":
                finById();
                System.out.println("查询指定文章操作");
                break;
            case "4":
                showAllArticle();
                System.out.println("查询全部文章操作");
                break;
            case "5":
                System.out.println("退出系统");
                System.out.println("查询全部文章操作");
                return;
            default:
                System.out.println("编号输入有误");
         }
        }
    }
    public  void writeArticle(){
        Article article = InputUtil.getInputObject(new Article(),"发表文章");
        //调用ArticleMapper将文章保存
        AticleMapper mapper = new AticleMapper();
        int num = articleMapper.insertArticle(article);
        //根据保存结果提示用户是否成功
        if (num>0){
            System.out.println("文章发表成功");
        }else {
            System.out.println("文章发表失败");
        }
    }
    public  void deleteById(){
        System.out.println("删除文章");
        System.out.println("请输入要删除的文章id:");
        Scanner scanner = new Scanner(System.in);
        int id = Integer.parseInt(scanner.nextLine());
        int num = articleMapper.deleteArticle(id);
        System.out.println(num>0?"删除成功":"删除失败");
    }
    public void finById(){
        System.out.println("查询文章");
        System.out.println("请输入要查询的文章id:");
        Scanner scanner = new Scanner(System.in);
        int id = Integer.parseInt(scanner.nextLine());
        Article article =articleMapper.findById(id);
        System.out.println("标题"+article.getTitle());
        System.out.println("内容"+article.getContent());
    }
    public  void showAllArticle(){
        List<Article> list = articleMapper.findAll();
        for (Article article : list){
            System.out.println("标题"+article.getTitle());
            System.out.println("内容"+article.getContent());
        }
    }


}
